package com.zx.erp.market.controller;


import com.github.pagehelper.PageInfo;
import com.zx.erp.annonation.MyLog;
import com.zx.erp.market.bean.Custom;
import com.zx.erp.market.service.CustomService;
import com.zx.erp.market.service.OrdersService;
import com.zx.erp.sys.bean.City;
import com.zx.erp.sys.bean.Job;
import com.zx.erp.sys.bean.Province;
import com.zx.erp.sys.bean.Users;
import com.zx.erp.sys.mapper.CityMapper;
import com.zx.erp.sys.service.CityService;
import com.zx.erp.sys.service.JobService;
import com.zx.erp.sys.service.ProvinceService;
import com.zx.erp.sys.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/market/customer")
@SessionAttributes({"provinceList","pb"})
public class CustomController {
    @Autowired
    private CustomService cbiz;
    @Autowired
    private CityService citybiz;
    @Autowired
    private ProvinceService pbiz;
    @Autowired
    private JobService jbiz;
    @Autowired
    private UsersService uBiz;
    @Autowired
    protected OrdersService obiz;

    //每次执行前会优先 将mid放入request范围
    @ModelAttribute
    public void initModuleData(HttpSession session) {
        session.removeAttribute("mid");
        session.setAttribute("mid", 6);
    }


    //市场部  经理

    @MyLog("分页查询所有（有模糊查询）操作")
    @RequestMapping("/findPageAll")
    public String findPageAll(Custom custom, Integer pid, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, Model model, Integer allot) {
        List<Integer> cidList = new ArrayList<Integer>();
        List<City> cityList = new ArrayList<City>();
        if (custom.getAddress() == null || custom.getAddress() == 0) {
            if (pid != null && pid != 0) {
                cityList = citybiz.findByPid(pid);
                for (City c : cityList) {
                    cidList.add(c.getId());
                }
            }
        } else {
            cidList.add(custom.getAddress());
            model.addAttribute("cid", custom.getAddress());
        }

        PageInfo<Custom> pb = cbiz.findPageAll(custom, cidList, allot, pageNum, 6);
        model.addAttribute("pb", pb);
        model.addAttribute("custom", custom);
        model.addAttribute("cid", custom.getAddress());
        List<Province> provinceList = pbiz.findAll();
        model.addAttribute("provinceList", provinceList);
        model.addAttribute("cityList", cityList);
        model.addAttribute("pid", pid);
        model.addAttribute("allot", allot);
        Job job = new Job();
        job.setJobDeptId(1);
        List<Job> jobList = jbiz.findAll(job);
        model.addAttribute("jobList", jobList);
        return "forward:/market/customer/customerList.html";
    }


   /* *
     * 通过省id查市
     * @param pid
     * @return
     * */
    @RequestMapping("/ajaxCityPid")
    @ResponseBody
    public List<City> ajaxCityPid(Integer pid) {
        List<City> cityList = citybiz.findByPid(pid);
        return cityList;
    }

    @MyLog("查询详情操作")
    @RequestMapping("/findBy")
    public String findBy(Integer id, Model model) {
        Custom custom = cbiz.findBy(id);
        model.addAttribute("custom", custom);
        return "forward:/market/customer/customerView.html";
    }

    /*@MyLog("查询详情操作")
    @RequestMapping("/findBy2")
    public String findBy2(String orderid,Model model){
        Orders order = obiz.findBy(orderid);
        Custom custom = cbiz.findBy(order.getCustomid());
        model.addAttribute("custom", custom);
        return "forward:/market/customer/customerView.html";
    }*/


    @MyLog("添加客户操作")
    @RequestMapping("/addCustom")
    public String addCustom(Custom custom, Integer uid, Model model) {
        custom.setCreateid(uid);
        custom.setCreatetime(new Date(System.currentTimeMillis()));
        boolean flag = cbiz.addCustom(custom);

        PageInfo<Custom> pb =(PageInfo<Custom>) model.getAttribute("pb");
        Integer pageNum=pb.getPages();
        if (pb.getTotal()%pb.getPageSize()==0){
            pageNum+=1;
        }
        if (flag) {
            return "redirect:/market/customer/findPageAll?pageNum="+pageNum;
        } else {
            return "";
        }
    }

   /* *
     * 跳到修改客户页面
     * @param id
     * @param model
     * @return*/

    @RequestMapping("/goEditCustom")
    public String goEditCustom(Integer id, Model model) {
        Custom custom = cbiz.findBy(id);
        model.addAttribute("custom", custom);
        List<City> cityList = citybiz.findByPid(custom.getCity().getPId());
        model.addAttribute("cityList2",cityList);
        return "forward:/market/customer/customerUpdate.html";
    }

    @MyLog("修改客户信息操作")
    @RequestMapping("/editCustom")
    public String editCustom(Custom custom,Model model) {
        boolean flag = cbiz.editCustom(custom);

        PageInfo<Custom> pb =(PageInfo<Custom>) model.getAttribute("pb");
        Integer pageNum=pb.getPageNum();

        if (flag) {
            return "redirect:/market/customer/findPageAll?pageNum="+pageNum;
        } else {
            return "";
        }
    }

    @MyLog("修改客户状态操作")
    @RequestMapping("/editStatus")
    public String editStatus(Integer id,Model model) {
        Custom custom = cbiz.findBy(id);
        if (custom.getStatus() == 1) {
            custom.setStatus(2);
        } else {
            custom.setStatus(1);
        }
        boolean flag = cbiz.editCustom(custom);
        PageInfo<Custom> pb =(PageInfo<Custom>) model.getAttribute("pb");
        Integer pageNum=pb.getPageNum();
        if (flag) {
            return "redirect:/market/customer/findPageAll?pageNum="+pageNum;
        } else {
            return "";
        }
    }

    /**
     * 通过职位id查用户
     * @param jobId
     * @return
*/
    @RequestMapping("/ajaxJobId")
    @ResponseBody
    public List<Users> ajaxJobId(Integer jobId) {
        Users user = new Users();
        user.setJobId(jobId);
        user.setStatusId(1);
        List<Users> userList = uBiz.findAll(user);
        return userList;
    }

    @MyLog("给市场部成员分配客户")
    @RequestMapping("/editFenPei")
    public String editFenPei(Integer uid, Integer id,Model model) {
        Custom custom = cbiz.findBy(id);
        custom.setDistractime(new Date(System.currentTimeMillis()));
        custom.setServiceid(uid);
        boolean flag = cbiz.editCustom(custom);
        PageInfo<Custom> pb =(PageInfo<Custom>) model.getAttribute("pb");
        Integer pageNum=pb.getPageNum();
        if (flag) {
            return "redirect:/market/customer/findPageAll?pageNum="+pageNum;
        } else {
            return "";
        }
    }
}
